//
//  QSAdBannerView.m
//  ZengLongSeSha
//
//  Created by apple on 2017/7/5.
//  Copyright © 2017年 Song. All rights reserved.
//

#import "QSAdBannerView.h"

@interface QSAdBannerView()<UIScrollViewDelegate>
/** 定时器 */
@property (nonatomic, strong) NSTimer *timer;

/** 滚动视图 */
@property (nonatomic, weak) UIScrollView *scrollView;

/** 图片数组 */
@property (nonatomic, strong) NSArray *picsArray;

/** 跳过按钮 */
@property (nonatomic, weak) UIButton *jumpOverBtn;
@end

@implementation QSAdBannerView
/**
 *  初始化广告轮播图
 */
- (instancetype)initBannerViewWithPictures:(NSArray *)picNameArray toView:(UIView *)targetView
{
    if (self = [super initWithFrame:targetView.bounds]) {
        self.backgroundColor = [UIColor whiteColor];
        // 添加到目标视图
        [targetView addSubview:self];
        
        self.picsArray = picNameArray;
        
        // 设置滚动视图
        UIScrollView *scrollView = [self setupScrollPicViewWithPictures:picNameArray];
        [self addSubview:scrollView];
        
        // 创建跳过按钮
        UIButton *jumpOverBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.qs_width - 80, 30, 60, 34)];
        [jumpOverBtn setTitle:@"跳过" forState:UIControlStateNormal];
        [jumpOverBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [jumpOverBtn setBackgroundColor:kUIColorCreateWith(0, 0, 0, 0.5)];
        jumpOverBtn.layer.cornerRadius = 5.0;
        jumpOverBtn.layer.masksToBounds = YES;
        [jumpOverBtn addTarget:self action:@selector(clickJumpOver) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:jumpOverBtn];
        self.jumpOverBtn = jumpOverBtn;
        
        // 设置定时器时间
        self.myTimeInterval = 2.0;
    }
    return self;
}

/**
 *  设置滚动视图
 */
- (UIScrollView *)setupScrollPicViewWithPictures:(NSArray *)pics
{
    // 创建scrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    scrollView.delegate = self;
    self.scrollView = scrollView;
    
    for (int i = 0; i < pics.count; i++) {
        UIImageView *imgView = [[UIImageView alloc] init];
        
        NSString *imageName = [pics objectAtIndex:i];
        if ([imageName hasPrefix:@"http"] || [imageName hasPrefix:@"https"]) {
            [imgView sd_setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
        } else {
            [imgView setImage:[UIImage imageNamed:imageName]];
        }
        
        // 计算imageView的x值
        CGFloat imgViewX = i * self.qs_width;
        imgView.frame = CGRectMake(imgViewX, 0, self.qs_width, self.qs_height);
        
        // 添加到scrollView
        [scrollView addSubview:imgView];
    }
    
    // 设置 scrollView的contentSize
    scrollView.contentSize = CGSizeMake(pics.count * self.qs_width, 0);
    // 隐藏滚动指示器
    scrollView.showsHorizontalScrollIndicator = NO;
    // scrollView的分页效果
    scrollView.pagingEnabled = YES;
    
    return scrollView;
}

/**
 *  创建计时器
 */
- (void)initImageTimer
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:self.myTimeInterval
                                              target:self
                                            selector:@selector(scrollNext)
                                            userInfo:nil
                                             repeats:YES];
    
    // 添加到运行时循环
    NSRunLoop *mainLoop = [NSRunLoop mainRunLoop];
    [mainLoop addTimer:_timer forMode:NSRunLoopCommonModes];
}

/**
 *  滚动到下一张
 */
- (void)scrollNext
{
    
    CGPoint offset = _scrollView.contentOffset;
    offset.x += self.qs_width;
    // 赋值回去
    [_scrollView setContentOffset:offset animated:YES];
    
    if (offset.x >= self.qs_width * self.picsArray.count) {
        [self clickJumpOver];
    }
}

#pragma mark - scrollView的代理方法
/**
 *  当scrollView开始减速的时候调用
 */
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    // 在最后一张中，拖动超过四分之一时自动跳过
    CGFloat pageOffset = scrollView.contentOffset.x + self.qs_width;
    if (pageOffset > (self.qs_width * self.picsArray.count + self.qs_width / 4)) {
        [self clickJumpOver];
        return;
    }
}

/**
 *  开始拖拽
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    // 让计时器无效
    [_timer invalidate];
    _timer = nil;
}

/**
 *  停止拖拽
 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [_timer fire];
    [self initImageTimer];
}

/**
 *  跳过广告页
 */
- (void)clickJumpOver
{
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(-self.qs_width, 0, self.qs_width, self.qs_height);
    } completion:^(BOOL finished) {
        // 关闭定时器
        [_timer invalidate];
        _timer = nil;
        
        // 调用block
        if (self.AdBannerViewDisappearBlock) {
            self.AdBannerViewDisappearBlock();
        }
        
        self.hidden = YES;
        [self removeFromSuperview];
    }];
}

#pragma mark - set
- (void)setHideJumpOverBtn:(BOOL)hideJumpOverBtn
{
    _hideJumpOverBtn = hideJumpOverBtn;
    self.jumpOverBtn.hidden = hideJumpOverBtn;
}

- (void)setJumpOverBgColor:(UIColor *)jumpOverBgColor
{
    _jumpOverBgColor = jumpOverBgColor;
    [self.jumpOverBtn setBackgroundColor:jumpOverBgColor];
}

- (void)setJumpOverBtnCornerRadius:(CGFloat)jumpOverBtnCornerRadius
{
    _jumpOverBtnCornerRadius = jumpOverBtnCornerRadius;
    self.jumpOverBtn.layer.cornerRadius = jumpOverBtnCornerRadius;
}

- (void)setJumpOverBtnTitleColor:(UIColor *)jumpOverBtnTitleColor
{
    _jumpOverBtnTitleColor = jumpOverBtnTitleColor;
    [self.jumpOverBtn setTitleColor:jumpOverBtnTitleColor forState:UIControlStateNormal];
}

- (void)setScrollViewNeedDelegate:(BOOL)scrollViewNeedDelegate
{
    _scrollViewNeedDelegate = scrollViewNeedDelegate;
    if (!scrollViewNeedDelegate) {
        self.scrollView.delegate = nil;
    }
}

- (void)setMyTimeInterval:(NSTimeInterval)myTimeInterval
{
    _myTimeInterval = myTimeInterval;
    
    if (self.timer.valid) {
        [self.timer invalidate];
        _timer = nil;
    }
    // 开启定时器
    [self initImageTimer];
}
@end
